package com.zsyh.jiujiu.weight;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.zsyh.jiujiu.ChengXinAdepter;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.adepter.CommonAdapter;
import com.zsyh.jiujiu.adepter.huiyuanAdepter;
import com.zsyh.jiujiu.bean.HuiYuanBean;
import com.zsyh.jiujiu.dialog.DiquPop;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import razerdp.basepopup.BasePopupWindow;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PopShaiXuan extends BasePopupWindow {


    private huiyuanAdepter huiyuanAdepter;
    private List<Integer> listone=new ArrayList<>();
    private ChengXinAdepter chengXinAdepter;
    private List<Integer> listtwo=new ArrayList<>();
    private OnSureClickListener onSureClickListener;
    private String deengji,chengxin,highprice,lowpricee,cityid;
    private FragmentActivity mContext;
    public PopShaiXuan(Context context,FragmentActivity mContext) {
        super(context);
        setPopupGravity(Gravity.RIGHT);
        this.mContext=mContext;
        RecyclerView huiyuanlist=findViewById(R.id.huiyuanlist);
        RecyclerView xinjiulist=findViewById(R.id.xinjiulist);
        TextView chongzhi=findViewById(R.id.chongzhi);
        TextView queding=findViewById(R.id.queding);
        TextView adress=findViewById(R.id.adress);
        EditText low=findViewById(R.id.low);
        EditText high=findViewById(R.id.high);
        RelativeLayout readreess=findViewById(R.id.readreess);
        readreess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiquPop popShaiXuan = new DiquPop(context,mContext);
                popShaiXuan.setOnSureClickListener(new DiquPop.OnSureClickListener() {
                    @Override
                    public void callback(String cityids,String name) {
                        cityid=cityids;
                        adress.setText(name);
                    }
                });
                popShaiXuan.showPopupWindow();
            }
        });

        List<HuiYuanBean> huibeean=new ArrayList<>();
        HuiYuanBean one=new HuiYuanBean();
        one.setName("一星会员");
        one.setId("1");

        HuiYuanBean two=new HuiYuanBean();
        two.setName("二星会员");
        two.setId("2");

        HuiYuanBean three=new HuiYuanBean();
        three.setName("三星会员");
        three.setId("3");
        huibeean.add(one);
        huibeean.add(two);
        huibeean.add(three);
        listone.add(0);
        listone.add(0);
        listone.add(0);

        huiyuanAdepter=new huiyuanAdepter(context,huibeean,listone);
        huiyuanlist.setLayoutManager(new GridLayoutManager(context,2));
        huiyuanlist.setAdapter(huiyuanAdepter);
        huiyuanAdepter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                setpostion(listone,position);
                huiyuanAdepter.notifyDataSetChanged();
                deengji=huibeean.get(position).getId();
            }
        });

        List<String> xinjiu=new ArrayList<>();
        xinjiu.add("全新");
        xinjiu.add("九成");
        xinjiu.add("八成");
        xinjiu.add("七成");
        xinjiu.add("七成以下");
        listtwo.add(0);
        listtwo.add(0);
        listtwo.add(0);
        listtwo.add(0);
        listtwo.add(0);
        chengXinAdepter=new ChengXinAdepter(context,xinjiu,listtwo);
        xinjiulist.setLayoutManager(new GridLayoutManager(context,3));
        xinjiulist.setAdapter(chengXinAdepter);
        chengXinAdepter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                setpostiontwo(listtwo,position);
                chengXinAdepter.notifyDataSetChanged();
                chengxin=xinjiu.get(position);
            }
        });


        chongzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adress.setText("所有区域");
                cityid="";
                highprice="";
                lowpricee="";
                deengji="";
                chengxin="";
                high.setText("");
                low.setText("");
                listone.clear();
                listone.add(0);
                listone.add(0);
                listone.add(0);
                huiyuanAdepter.notifyDataSetChanged();
                listtwo.clear();
                listtwo.add(0);
                listtwo.add(0);
                listtwo.add(0);
                listtwo.add(0);
                listtwo.add(0);
                chengXinAdepter.notifyDataSetChanged();
            }
        });

        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSureClickListener.callback(deengji,chengxin,low.getText().toString(),high.getText().toString(),cityid);
                dismiss();
            }
        });
    }


    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.pop_shaixuan);
    }



    public void setpostion(List<Integer> list, int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.set(postion, 1);
            } else {
                list.set(i, 0);
            }
        }

    }

    public void setpostiontwo(List<Integer> list, int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.set(postion, 1);
            } else {
                list.set(i, 0);
            }
        }

    }

    public void setOnSureClickListener(OnSureClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    public interface OnSureClickListener {
        void callback(String userType,String newType,String startPrice,String endPrice,String cityId);
    }


}
